"ui";
//2019-1-18
const versionCode = 5;
const Spiccode = "v0.8.0";

const share = "QQ工具箱:\n" +
    "一款方便快捷的QQ辅助应用APP，内有多种小功能，欢迎下载使用!\n" +
    "欢迎加入QQ工具箱官方群，群号码：984523553。\n" +
    "下载链接\n" +
    "https://www.lanzous.com/b309093";
const QAQ = "1.这个软件支持哪些手机？\n答：一般安卓系统7.0以上的都支持，但也有可能存在个别手机的兼容问题，如在使用过程中发现有问题，请及时反馈。\n2.苹果手机可以用吗？\n答：不可以。\n3.为什么按开始了，没有反应？\n答：本软件功能实现基于无障碍服务，如果你没有开启，是无法运行某个功能的。\n4.此软件收费吗？\n答：不收，以后也不收，作者就喜欢开发不赚钱的软件，就是这么任性👻";
const INFO = "本软件为免费软件！\n如果你从任何地方购买的，那么您很可能上当了。\n此软件中使用到的某些功能模块，是在凹凸结实群中收集的，由本人适配。\n\n此软件使用凹凸结实开发";
const releaseNotes = "QQ工具箱，是一款免费的软件，您的捐赠将促进软件持续更新，金额不限，您的支持对独立软件开发者很重要，请帮忙多多宣传。"; //\n\nps:给作者买包辣条可以开发智力，激发潜能，以后才有动力维护及开发其他功能。";
var progressDialog = null;
var cookie;
const Colors = ["#e91e63", "#ab47bc", "#5c6bc0", "#7e57c2", "##2196f3", "#00bcd4",
    "#26a69a", "#4caf50", "#8bc34a", "#ffeb3b", "#ffa726", "#78909c", "#8d6e63"];

var storage = storages.create("QqTools_data");
//var Data = storage.get("Data", {time: "",cookie: ""});
var Gg = storage.get("gg"); //存储公告
// storages.remove("Data");
//功能列表
const F_List = [{
    id: "自动名片赞",
    text: "自动名片赞，可以帮助你一个一个回赞，解放你的双手",
    code: "autozan",
    bg: "#808080"
}, {
    id: "文字转语音发送",
    text: "文字转语音发送，这个在好友或者群聊界面的文字输入框里输入文字，然后会以语音的形式发送给对方",
    code: "wenzhuan",
    bg: "#808080"
}, {
    id: "QQ连发器",
    text: "QQ连发器,刷屏必备神器，因为没有人的手速快过脚本。请小心使用，过度刷屏容易被T",
    code: "lianfaqi",
    bg: "#808080"
}, {
    id: "空间蓝色字体",
    text: "把文字内容复制进来，点生成即可，然后到空间粘贴发送，你的字体就会变成蓝色的啦",
    code: "lanse",
    bg: "#808080"
}, {
    id: "强制聊天",
    text: "这个功能用于跟还没有添加好友的人进行聊天，强制性的不需要对方同意好友(对某些人可能失效)",
    code: "qiangzhi",
    bg: "#808080"
}, {
    id: "自定义分享",
    text: "强大的自定义分享功能，你可以完美模仿王者荣耀的邀请链接，别人打开的却是另一个页面。当然其他的也可以，更多功能请进群询问",
    code: "zdyfx",
    bg: "#808080"
}, {
    id: "小数字",
    text: "小功能，用于生成类似于这样的小数字¹²³₁₂₃ ，至于玩法自行探究",
    code: "xsz",
    bg: "#808080"
}, {
    id: "支付宝语音到账",
    text: "100% 模拟支付宝语音到账声音，可用于短信提示声，来电铃声，装B必备。",
    code: "zhifubao",
    bg: "#808080"
}, {
    id: "QQ群一键签到",
    text: "一键极速签到, 告别频繁手动签到, 该功能支持自定义签到标题及地区",
    code: "zhifubao",
    bg: "#808080"
}];

ui.statusBarColor("#a3a1a1");
ui.layout(
    <drawer id="drawer" bg="#dcdcdc">
        <vertical>
            <appbar w="*" bg="#a3a1a1">
                <toolbar id="toolbar" title="QQ工具箱" />
            </appbar>
            <viewpager id="viewpager" layout_weight="1">
                <frame>
                    <grid id="list" spanCount="1" h="*" fastScrollEnabled="true" scrollbars="vertical" scrollbarStyle="outsideInset">
                        <vertical>
                            <card w="*" h="auto" margin="10 5 10 5" gravity="center" cardCornerRadius="5dp" cardBackgroundColor="{{this.bg}}"
                                layout_gravity="center" foreground="?selectableItemBackground">
                                <vertical>
                                    <text text="{{this.id}}" layout_gravity="left" textSize="18" padding="5 5 5 0" textColor="#ffffff" bg="?selectableItemBackgroundBorderless" />
                                    <text text="{{this.text}}" layout_gravity="left" padding="5 5 5 5" textSize="16" textColor="#000000" w="auto" />
                                </vertical>
                            </card>

                        </vertical>
                    </grid>
                </frame>
                <frame>
                    <vertical layout_weight="1">
                        <linear orientation="vertical" >
                            <img id="me" src="http://qlogo1.store.qq.com/qzone/1641763174/1641763174/100"
                                w="100" h="100" radius="20dp" marginTop="15" scaleType="fitXY" layout_gravity="center" />
                            <text text="QQ工具箱 {{Spiccode}}" textStyle="bold" textColor="black" textSize="16sp" marginTop="16" gravity="center" />
                        </linear>
                        <vertical marginTop="15">

                            <horizontal margin="5 15 0 0" bg="#ffffff">
                                <img src="file://./res/ic_card_giftcard_black_48dp.png" w="35" h="35" />
                                <text text="友情赞助" id="zanzhu" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 2 0 0" bg="#ffffff">
                                <img src="file://./res/ic_touch_app_black_48dp.png" w="35" h="35" />
                                <text text="官方交流群" id="qun" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 15 0 0" bg="#ffffff">
                                <img src="file://./res/ic_help_outline_black_48dp.png" w="35" h="35" />
                                <text text="使用帮助" id="help" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 2 0 0" bg="#ffffff">
                                <img src="file://./res/ic_share_black_48dp.png" w="35" h="35" />
                                <text text="分享软件" id="fen" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 2 0 0" bg="#ffffff">
                                <img src="file://./res/ic_message_black_48dp.png" w="35" h="35" />
                                <text text="意见反馈" id="jianyi" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 2 0 0" bg="#ffffff">
                                <img src="file://./res/ic_refresh_black_48dp.png" w="35" h="35" />
                                <text text="检测更新" id="up" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 2 0 0" bg="#ffffff">
                                <img src="file://./res/ic_info_outline_black_48dp.png" w="35" h="35" />
                                <text text="关于软件" id="info" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>

                            <horizontal margin="5 15 0 0" bg="#ffffff">
                                <img src="file://./res/ic_exit_to_app_black_48dp.png" w="35" h="35" />
                                <text text="退出软件" id="exit" textSize="25dp" w="*" bg="?attr/selectableItemBackground" clickable="true" />
                            </horizontal>
                        </vertical>
                    </vertical>
                </frame>
            </viewpager>
            <appbar h="50" w="*" bg="#a3a1a1">
                <tabs id="tabs" />
            </appbar>
        </vertical>
    </drawer>
);
activity.setSupportActionBar(ui.toolbar);
ui.viewpager.setTitles(["功能", "我的"]);
ui.tabs.setupWithViewPager(ui.viewpager);
ui.list.setDataSource(F_List);

// var exis1 = setInterval(() => {
//     if (isVpnUsed() || isWifiProxy()) {
//         exit();
//     }
// }, 800);

var thr = threads.start(function () {
    if (!isVpnUsed() && !isWifiProxy()) {
        http.get("https://www.apiopen.top/addStatistics?appKey=bb915a1e008921683029b94219b7d9bf&type=com.qqtools.bjzf&typeId=5&count=1");
        if (device.sdkInt < 24) {
            // toast("该软件有部分功能需要7.0以上，可能有部分功能不支持您的手机");
            sleep(1500);
            //exit();
        }
        sleep(500);
        updata(0, 1);
    } else {
        // exit();
    }
});

ui.list.on("item_click", function (name) {
    QuoteCode(name.code);
});
ui.me.click(() => {
    toast("快赞我一波(*ﾟ∀ﾟ*)");
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "mqqapi://card/show_pslcard?&uin=1641763174"
    });
});
ui.zanzhu.click(() => {
    dialogs.build({
        title: "请选择打赏的方式",
        content: releaseNotes,
        neutral: "取消",
        negative: "支付宝",
        positive: "微信",
        cancelable: false
    })
        .on("neutral", () => {
            toast("QQ工具箱的发展，离不开您的支持")
        })
        .on("negative", () => {
            alipay("fkx06321tasf2kjzl3dki78");
        })
        .on("positive", () => {
            QuoteCode("weixin");
        })
        .show();
});

ui.qun.click(() => {
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "mqqapi://card/show_pslcard?card_type=group&uin=984523553",
        packageName: "com.tencent.mobileqq",
    });
});

ui.help.click(() => {
    dialogs.build({
        title: "常见问题QAQ",
        content: QAQ,
        positive: "我知道了",
        cancelable: false
    })
        .show();
});

ui.jianyi.click(() => {
    QuoteCode("wenti");
});
ui.fen.click(() => {
    dialogs.build({
        title: "是否分享给好友？",
        content: null,
        positive: "确定",
        negative: "取消",
        cancelable: false
    })
        .on("positive", () => {
            app.startActivity({
                action: "android.intent.action.SEND",
                type: "text/*",
                extras: {
                    "android.intent.extra.TEXT": share
                },
                packageName: "com.tencent.mobileqq",
                className: "com.tencent.mobileqq.activity.JumpActivity"
            });
        })
        .show();
});
ui.up.click(() => {
    threads.start(function () {
        showProgress();
        if (!updata(1)) toast("检测更新失败(╥ω╥`)");
    });
});
ui.up.on("long_click", () => {
    app.openUrl("https://www.lanzous.com/b309093");
});
ui.info.click(() => {
    dialogs.build({
        title: "关于工具箱" + Spiccode,
        content: INFO,
        positive: "我知道了",
        cancelable: false
    })
    .show();
});

ui.exit.click(() => {
    dialogs.confirm("提示", "您真的要退出吗？", (a) => {
        if (a) {
            ui.finish();
        }
    });
});

function showProgress() {
    if (!progressDialog) {
        progressDialog = dialogs.build({
            title: "正在检测更新...",
            progress: {
                max: -1
            },
            negative: "取消更新",
            cancelable: false
        })
            .on("negative", () => {
                dismissProgress();
            })
            .show();
    }
}

function dismissProgress() {
    if (progressDialog) {
        progressDialog.dismiss();
    }
}


//获取公告
function check() {
    let hand = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "text/html, application/xhtml+xml, */*",
        "Connection": "Keep-Alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31",
        "Host": "w.eydata.net",
        "Accept-Encoding": "gzip",
        "Content-Length": "0"
    }
    //http.post("http://w.eydata.net/1d6f3ba11a70d37a", hand, function(res) {
    http.get("http://1024.my81.club/Notice.json", null, function (res) {
        let res = res.body.json();
        // log(res);
        if (res.code == 200) {
            if (Gg == null || Gg.code < res.is && Gg.bool != true) {
                ui.run(() => {
                    dialogs.build({
                        title: res.title,
                        content: res.content,
                        checkBoxPrompt: "不再提示",
                        cancelable: false,
                        positive: "我知道了",
                        cancelable: false
                    })
                        .on("check", (checked) => {
                            storage.put("gg", {
                                "code": res.is,
                                "bool": true
                            });
                        })
                        .show();
                })
            }
        }
    });
}

//获取更新信息
function updata(is, gg) {
    var up = threads.disposable();
    threads.start(function () {
        if (!isVpnUsed() && !isWifiProxy()) {
            try {
                let hand = {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Accept": "text/html, application/xhtml+xml, */*",
                    "Connection": "Keep-Alive",
                    "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31",
                    "Host": "w.eydata.net",
                    "Accept-Encoding": "gzip",
                    "Content-Length": "0"
                }
                var res = http.post("http://w.eydata.net/ff72da594ec80515", hand);
                //log(res.body.json())
                //let res = http.get("http://1024.my81.club/qqtools.json");
                if (res.statusCode != 200) {
                    toast("更新失败: " + res.statusCode + " " + res.statusMessage);
                    return false;
                }
                let json = res.body.json();
                if (versionCode < json["versionCode"]) {
                    if (json["code"] == 200) {
                        dismissProgress();
                        up.setAndNotify(true);
                        dialogs.build({
                            title: json["title"] + json["versionName"],
                            content: json["content"],
                            neutral: "取消",
                            negative: "立即更新",
                            cancelable: false
                        })
                            .on("negative", () => {
                                app.openUrl(json["url"]);
                            })
                            .show();
                    }
                } else {
                    if (is) toast("已是最新版");
                    dismissProgress();
                }
                if (gg) check();
            } catch (e) {
                dismissProgress();
                //toast("检测更新失败(~_~;)");
            }
        }
    });
    return up.blockedGet();
}

function nTimeNum() {
    return new java.text.SimpleDateFormat("yyyyMMdd").format(new Date());
}

function alipay(code) {
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "alipayqr://platformapi/startapp?saId=10000007&qrcode=" +
            "HTTPS://QR.ALIPAY.COM/" + code
    });
}

function QuoteCode(Name) {
    let code = "./lib/" + Name + ".js";
    engines.execScriptFile(code);
}

function isWifiProxy() {
    var IS_ICS_OR_LATER = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    if (IS_ICS_OR_LATER) {
        var proxyAddress = java.lang.System.getProperty("http.proxyHost");
        var portStr = java.lang.System.getProperty("http.proxyPort");
        var proxyPort = java.lang.Integer.parseInt((portStr != null ? portStr : "-1"));
    } else {
        var proxyAddress = android.net.Proxy.getHost(this);
        var proxyPort = android.net.Proxy.getPort(this);
    }
    return (!android.text.TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);
}

function isVpnUsed() {
    var niList = java.net.NetworkInterface.getNetworkInterfaces();
    if (niList != null) {
        var list = java.util.Collections.list(niList);
        for (let i = 0; i < list.size(); i++) {
            var Nane = list.get(i).getName();
            if ("tun0".equals(Nane) || "ppp0".equals(Nane)) return true;
        }
    }
    return false;
}